<?php
namespace app\appauth\controller;
use app\appauth\model\AuthM;
use app\rbac\controller\Admin;
use app\rbac\model\AdminM;
use think\Db;
use think\Session;
use app\base\controller\Base;

class Auth extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'权限不够']);
        }
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new AuthM();
            $model->add($data);
            return json(['code'=>$model->code,'msg'=>$model->msg]);
        }
        $this->assign(array(
            '_page_title'=>'添加应用',
            '_page_title_english'=>'Auth add',
            '_btn_title'=>'应用列表',
            '_btn_link'=>'lst',
            '_page_ico'=>'iconfa-bolt',
        ));
        return $this->fetch();
    }



    /**
     * 编辑页面
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'权限不够']);
        }

        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new AuthM();
            $model->edit($data);
            return json(['code'=>$model->code,'msg'=>$model->msg]);
        }
        $data = Db::table('app_auth')->where(['is_usable'=>1,'app_id'=>$id])->field('app_id,org_name')->find();
        $this->assign(array(
            '_page_title'=>'编辑应用',
            '_page_title_english'=>'Auth edit',
            '_btn_title'=>'应用列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>' iconfa-bolt',
            'data'=>$data,
        ));
        return $this->fetch();
    }

    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'权限不够']);
        }

        $model = new AuthM();
        $data = $model->lst();
        $page = $data->render();
        $this->assign(array(
            '_page_title'=>'应用列表',
            '_page_title_english'=>'Auth list',
            '_btn_title'=>'添加应用',
            '_btn_link'=>url('add'),
            '_page_ico'=>' iconfa-bolt',
            'data'=>$data,
            'page'=>$page,
        ));
        return $this->fetch();
    }


    /**
     * 删除数据
     * @param $id
     */
    public function del()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'权限不够']);
        }

        $id = $this->request->post('id');
        $model = new AuthM();
        $model->del($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }


}
